package com.sise.rsc.repository;

import com.sise.rsc.entity.Task;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface TaskRepository extends JpaRepository<Task,Integer> {

    @Query(" from Task t where t.courier.no=?1 and t.state !=?2")
    List<Task> findAllByCourierNoAndNoState(String no,int state);

    @Query("from Task t WHERE t.aPackage.no=?1 and t.taskName=?2")
    Task findTaskByTaskNameAndAPackageNo(String taskName,String pNo);

    @Query("from Task t WHERE t.aPackage.no=?1 and  t.state=?2 and t.taskName=?3")
    Task findTaskByTaskNameAndStateAndAPackageNo(String pNo ,int state,String taskName);
    //查询某一个快递员的所有任务信息
    @Query("from Task t where t.courier.id=:courierId")
    List<Task> findAllTaskByCourierId(int courierId);
    //查询所有任务量
    @Query(" from Task t where t.state=1")
    List<Task> findAllTaskByState();
    //查询所有任务量根据pIdp
    @Query(" select count (*) from Task t where t.state=0 and t.courier.id=:cId")
    int findAllTaskByStateAndcId(int cId);

    @Query("select count (*) from Task t where taskName=?1 and  t.aPackage.id=?2")
    int findTaskSumByTaskNameAndPackageId(String taskName,int pId);

    @Modifying
    @Transactional
    @Query("update Task t set t.state=?1 where t.taskName=?2 and t.aPackage.id=?3 and t.courier.id=?4")
    void updateTaskState(int state,String taskName,int pId,int cId);

}
